package com.f5.management.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.f5.management.entity.AdminUser;
import com.f5.management.entity.Permission;
import com.f5.management.entity.Role;
import com.f5.management.entity.dto.AdminUserAndRoleAndPermissionDto;
import com.f5.management.entity.dto.AdminUserAndRoleDto;
import com.f5.management.entity.dto.RoleFormDto;
import com.f5.management.service.AdminUserService;
import com.f5.management.service.PermissionService;
import com.f5.management.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName 吴
 * @Description 权限模块
 * @Author director
 * @Date 2021/4/1 20:50
 * @Version 1.0
 **/


@RestController
public class AdminController {

    //注入AdminUserService
    @Autowired
    AdminUserService adminUserService;

    @Autowired
    RoleService roleService;

    @Autowired
    AdminUser  user;

    @Autowired
    PermissionService permissionService;

    //返回前台一个用户的信息
    @RequestMapping("/getUser")
    public AdminUser getUser(HttpServletRequest request){
        String user = (String)request.getSession().getAttribute("user");
        AdminUser adminUser = adminUserService.getAdminUser(user);
        System.out.println(adminUser);
        return adminUser;
    }


    //登录认证
    @RequestMapping("/login")
    public String test(String user, String password, HttpServletRequest request){
        //测试输出
        System.out.println("账号："+user);
        System.out.println("密码："+password);
        UsernamePasswordToken token = new UsernamePasswordToken(user,password);
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(token);
            request.getSession().setAttribute("user", user);

            return "登录成功";
        }catch (UnknownAccountException e){
            return "账号信息不正确";
        }catch (IncorrectCredentialsException e){
            return "密码错误";
        }catch (AuthenticationException e){
            return  "认证信息发生异常";
        }

    }


    //查询管理员集合信息
    @RequiresRoles("admin")
    @RequiresPermissions("查看权限")
    @RequestMapping("/info")
    public List<AdminUser> getAdminUserRoleInfo(AdminUserAndRoleDto dto){

        return  adminUserService.getAdminUserLists(dto);
    }

    //查询管理员集合信息
    @RequiresRoles("admin")
    @RequiresPermissions("修改权限")
    @RequestMapping("/updateAdminUser")
    public String updateAdminUserState(AdminUser adminUser){

        adminUserService.updateAdminUserState(adminUser);

        return  "修改成功";
    }

    //返回页面角色信息集合
    @RequestMapping("/getRoles")
    public List<Role> getRoleLists(){

       return roleService.selectRoleLists();
    }

    //添加一个用户
    @RequiresRoles("admin")
    @RequiresPermissions("添加权限")
    @RequestMapping("/submit")
    public String submitAdminUser(AdminUser adminUser,int select,String truth_password ){
        System.out.println("adminUser--"+adminUser);
        System.out.println("select--"+select);
        System.out.println("truth_password--"+truth_password);
        return adminUserService.insertUser(adminUser, select, truth_password);
    }


    //返回所有的权限信息
    @RequestMapping("/getPermission")
    public List<Permission> getPermission(HttpServletRequest req){

        String user =(String)req.getSession().getAttribute("user");
        System.out.println(user);
        return permissionService.getPermissionList();
    }

    //删除用户
//    @RequiresRoles("admin")
    @RequiresPermissions("删除权限")
    @RequestMapping("/delete")
    public  String deleteAdminUser(String  account){

       adminUserService.deleteAdminUser(account);

        return  adminUserService.deleteAdminUser(account);
    }


    //返回用户的权限
    @RequestMapping("/getUserPermissions")
    public  List<Permission>  getAdminUserPermissions(HttpServletRequest request){

        String account = (String)request.getSession().getAttribute("user");

      return   permissionService.getPermissionsByAccount(account);
    }

    //返回前台所有权限
    @RequestMapping("/getRolesAndPermissions")
    public List<AdminUserAndRoleAndPermissionDto> getalldRoleAndPermission(){


        return  permissionService.getAdminUserAndRoleAndPermissionDto();
    }

    //Role.vue页面请求，获得所有角色权限信息
//    @RequiresRoles("admin")
    @RequestMapping("/getRolesAndPermissionsToRoleVue")
    public List<Role> getalldRoleAndPermissionToRoleVue(){

        return  roleService.getRolesAndPermissions();
    }

    //Role.vue页面请求，获得所有角色权限信息,进行添加角色操作
//    @RequiresRoles("admin")
    @RequiresPermissions("添加权限")
    @RequestMapping("/submitPermissions")
    public String getPermissions(@RequestBody RoleFormDto dto){

        return roleService.insetRloe(dto);

    }


    //删除一个Role
//    @RequiresRoles("admin")
    @RequiresPermissions("删除权限")
    @RequestMapping("/deleteRole")
    public String deleteRole(@RequestBody RoleFormDto dto){
        return  roleService.deleteRole(dto);
    }
}
